//
//  GQSetPwdController.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/5.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQSetPwdController.h"
#import "GQRegisterCPwdParam.h"
#import "GQRegisterCPwdResult.h"
#import "GQRegisterTool.h"
#import "NSString+Hash.h"
#import "TipView.h"

#define kLoginTextHeight 44
#define kLoginTextMargin 10
#define kLoginViewX 0
#define kLoginViewY 10

@interface GQSetPwdController ()

@property (strong, nonatomic) UITextField *setPwdTextField;
@property (strong, nonatomic) UIButton *confrimBtn;
@property (strong, nonatomic) UIView *containView;

@end

@implementation GQSetPwdController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:236 / 255.0 green:236 / 255.0 blue:236 / 255.0 alpha:1.0];
    self.view.frame = CGRectMake(0, kNavigatationBarHeight, kScreenWidth, kScreenHeight - kNavigatationBarHeight);
    [self setUpPhoneVertifyView];
    [self setUpNextBtn];
}

- (void)setUpPhoneVertifyView
{

    UIView *containView = [[UIView alloc] initWithFrame:CGRectMake(0, kNavigatationBarHeight + kLoginTextMargin, kScreenWidth, kLoginTextHeight)];
    containView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:containView];
    
    UIImageView *phoneImage =[[UIImageView alloc] initWithImage:[UIImage imageWithOriginalName:@"login_mobile"]];
    phoneImage.frame = CGRectMake(kLoginTextMargin, 4.5, 35, 35);
    [containView addSubview:phoneImage];
    
    UITextField *setPwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(kLoginTextMargin + phoneImage.width, 0, kScreenWidth - kLoginTextMargin * 2 - phoneImage.width - 20, kLoginTextHeight)];
    setPwdTextField.backgroundColor = [UIColor whiteColor];
    setPwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    setPwdTextField.placeholder = @"设置新密码";
    setPwdTextField.secureTextEntry = YES;
    self.setPwdTextField = setPwdTextField;
    
    UIButton *btnAccessEye = [[UIButton alloc] init];
    btnAccessEye.width = 20;
    btnAccessEye.height = 20;
    btnAccessEye.x = kScreenWidth - kLoginTextMargin - btnAccessEye.width;
    btnAccessEye.y = (containView.height - btnAccessEye.width) / 2;
    btnAccessEye.backgroundColor = [UIColor greenColor];
    
    [containView addSubview:setPwdTextField];
    [containView addSubview:btnAccessEye];
    self.containView = containView;
    
}

- (void)setUpNextBtn
{
    
    UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(kLoginTextMargin, CGRectGetMaxY(self.setPwdTextField.frame) + kLoginTextMargin / 2, 200, 15)];
    tipLabel.text = @"密码6-12位区分大小写";
    tipLabel.font = [UIFont systemFontOfSize:11];

    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(kLoginTextMargin, CGRectGetMaxY(self.containView.frame) + kLoginTextMargin, (kScreenWidth - kLoginTextMargin * 2), 40)];
    [btn setBackgroundColor:[UIColor colorWithRed:18 / 255.0 green:38 / 255.0 blue:58 / 255.0 alpha:1.0]];
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 5;
    [btn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:tipLabel];
    [self.view addSubview:btn];
    self.confrimBtn = btn;
}

#pragma mark - 点击确定按钮
- (void)confirmBtnClick
{
    
    
    NSString *saltStr = [self.setPwdTextField.text stringByAppendingString:kSaltString];
    [GQRegisterCPwdParam sharedGQRegisterCPwdParam].pass = [saltStr hmacMD5StringWithKey:@"hexingkeji"];
    [GQRegisterTool changePasswordWithParam:[GQRegisterCPwdParam sharedGQRegisterCPwdParam] success:^(GQRegisterCPwdResult *result) {
//        if ([result.errorCode isEqualToString:@"2004"]) {
//            [TipView showToView:self.view tipImage:@"login_sad" tipText:@"账户尚未注册"];
//        }else if([result.errorCode isEqualToString:@"2004"])
        switch ([result.errorCode integerValue]) {
            case 2004:
                [TipView showToView:self.view tipImage:[UIImage imageNamed:@"login_sad"] tipText:result.message];
                break;
            case 2015:
                [TipView showToView:self.view tipImage:[UIImage imageNamed:@"login_sad"] tipText:result.message];
                break;
            case 2002:
                [TipView showToView:self.view tipImage:[UIImage imageNamed:@"login_sad"] tipText:result.message];
                break;
            case 2001:
                [TipView showToView:self.view tipImage:[UIImage imageNamed:@"login_sad"] tipText:result.message];
                break;
            case 0:
                [TipView showToView:self.view tipImage:[UIImage imageNamed:@"login_smile"] tipText:@"修改成功"];
                break;
                
            default:
                break;
        }
    } failure:^(NSError *error) {
        
    }];
}

@end
